package com.handpay.nocardpay.baselibrary.qualificationinfo.widgets;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.qualificationinfo.comm.CommonHelper;
import com.handpay.nocardpay.baselibrary.utils.BitmapLoaderNew;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;

/**
 * 资质照片布局
 */
public class QualificationPhotoView extends RelativeLayout {
    public enum Statu {
        NoUpload, Upload, UploadSuccess, UploadFailed
    }
    private LayoutInflater mInflater;
    private Statu mStatu;
    public boolean isLoadSuccess = false;
    private int mDefaultPicShown;
    //删除
    private ImageView mIvTip,mIvPhoto,mIvUpload,iv_camare;
    private String mOutputFileUri;
    private UploadClickListener mUpListener;
    private RelativeLayout relaPictureRoot = null;
    private ProgressImageView progressimageview=null;

    public QualificationPhotoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context){
        mInflater = LayoutInflater.from(context);
        mInflater.inflate(R.layout.layout_confirm_photo, this);
        relaPictureRoot = (RelativeLayout) this.findViewById(R.id.relaPictureRoot);
        mIvTip = (ImageView) this.findViewById(R.id.tip);
        iv_camare=(ImageView)this.findViewById(R.id.iv_camare);
        // 点击右上角。问号的状态，显示默认图片；删除状态，删除当前图片.其他状态点击无效
        mIvTip.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果当前没上传，点击删除按钮重置当前状态
                if (Statu.Upload == mStatu) {
                    mOutputFileUri = null;
                    mIvPhoto.setImageResource(mDefaultPicShown);
                    mIvTip.setVisibility(View.INVISIBLE);
                    iv_camare.setVisibility(View.VISIBLE);
                    mStatu = Statu.NoUpload;
                }
                CommonHelper.deletePicture(mOutputFileUri);
            }
        });
        iv_camare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                takePictureRollBackListener();
            }
        });
        mIvUpload = (ImageView) this.findViewById(R.id.iv_upload);
        mIvUpload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                takePictureRollBackListener();
            }
        });
        // 当里面没图片的时候拍照。当有图片的时候，进入大图
        mIvPhoto = (ImageView) this.findViewById(R.id.iv_photo);
        mIvPhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(mOutputFileUri)){
                    checkBigPictureRollBackListener(mOutputFileUri);
                }else{
                    if (mStatu == Statu.UploadFailed || mStatu == Statu.NoUpload) {
                        takePictureRollBackListener();
                    }
                }
            }
        });
        progressimageview=(ProgressImageView)findViewById(R.id.number_circle);
    }

    /**
     * 初始化数据
     * @param defaultPicShown 默认背景图片
     * @param picUri 需要加载的图片uri
     * @param status 当前图片审核状态
     * @param uploadListener 操作监听
     */
    public void initData(int defaultPicShown, String picUri, int status, UploadClickListener uploadListener) {
        mDefaultPicShown = defaultPicShown;
        mIvPhoto.setBackgroundResource(defaultPicShown);
        mUpListener = uploadListener;
        //加载地址为空即认为初始状态
        if (!TextUtils.isEmpty(picUri)) {
            mOutputFileUri = picUri;
            if (status == 0) {//审核中
                setCkeckingStatusView();
            } else if (status == 1) {//审核通过--只有整体审核通过才隐藏图片状态
                setSuccessStatusView();
            } else if (status == 2) {//审核失败
                setErrorStatusView();
            }
            loadImage(mIvPhoto,picUri);
        } else {
            //没有数据 待补件
            setDefaultSatatusView();
        }
    }

    private void loadImage(final ImageView imageView,String uri){
        HPLog.i(HPLog.LFP_TAG,"-loadImage:uri:"+uri);
        // 确保该view的大小不可能为0
        measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        BitmapLoaderNew.BitmapLoaderListener listener = new BitmapLoaderNew.BitmapLoaderListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                HPLog.i(HPLog.LFP_TAG, "onLoadingComplete");
                loadedImage = ThumbnailUtils.extractThumbnail(loadedImage, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
                if (loadedImage == null) {
                    HPLog.i(HPLog.LFP_TAG, "loadedImage==null");
                }
                isLoadSuccess = true;
                mIvPhoto.setVisibility(View.VISIBLE);
                mIvPhoto.setImageBitmap(loadedImage);
            }
            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                super.onLoadingFailed(imageUri, view, failReason);
                HPLog.i(HPLog.LFP_TAG, "onLoadingFailed");
                isLoadSuccess = false;
            }

            @Override
            public void onLoadingStarted(String imageUri, View view) {
                super.onLoadingStarted(imageUri, view);
                mIvPhoto.setVisibility(View.INVISIBLE);
            }
        };
        //暂时不需要
        ImageLoadingProgressListener loadingProgressListener=new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String s, View view, int i, int i1) {
                HPLog.i(HPLog.LFP_TAG,"onProgressUpdate::i="+i+",i1="+i1);
                progressimageview.setCurrentProgress(i);
            }
        };

        BitmapLoaderNew.getInstance(getContext()).loadImage(uri,listener);
    }


    private void takePictureRollBackListener(){
        if(mUpListener!=null){
//            mIvPhoto.setImageResource(mDefaultPicShown);
            mUpListener.onUploadClickListener(QualificationPhotoView.this);
        }
    }

    private void checkBigPictureRollBackListener(String msg){
        if(mUpListener!=null){
            mUpListener.checkBigPitcure(msg);
        }
    }

    //默认状态{显示设置背景，顶部操作图片隐藏，右下方图片显示}
    private void setDefaultSatatusView(){
        mIvPhoto.setVisibility(View.VISIBLE);
        mIvPhoto.setImageResource(mDefaultPicShown);
        mIvTip.setVisibility(View.INVISIBLE);
        mIvTip.setImageResource(R.drawable.icon_qualification_delete);
        iv_camare.setVisibility(View.VISIBLE);
        mStatu = Statu.NoUpload;
    }

    //审核中视图{右下角图标隐藏，顶部操作按钮隐藏，默认背景图隐藏}
    private void setCkeckingStatusView(){
        iv_camare.setVisibility(View.INVISIBLE);
        mIvTip.setVisibility(View.INVISIBLE);
        mStatu = Statu.Upload;
    }

    //审核成功{右下角图标隐藏，顶部操作按钮显示【显示勾】，默认背景图隐藏【显示加载图片,加载不出来直接显示白色底】}
    private void setSuccessStatusView(){
        //隐藏右下角图标
        iv_camare.setVisibility(View.VISIBLE);
        //顶部成功勾勾
        mIvTip.setVisibility(View.VISIBLE);
        mIvTip.setImageResource(R.drawable.success);
        mStatu = Statu.UploadSuccess;
        isLoadSuccess = true;
    }
    //审核失败{右下角图片显示，顶部操作按钮显示【红色叉】，默认背景图隐藏【显示加载图片,加载不出来直接显示白色底】}
    private void setErrorStatusView(){
        //显示右下角图标
        iv_camare.setVisibility(View.VISIBLE);
        //顶部红色叉叉
        mIvTip.setVisibility(View.VISIBLE);
        mIvTip.setImageResource(R.drawable.error);
        //最外红色边框
        relaPictureRoot.setBackgroundResource(R.drawable.qualification_picture_error_background);
        mStatu = Statu.UploadFailed;
    }

    //设置本地照片地址{此时应显示顶部删除操作按钮，右下角图标依然显示}
    public void setPic(String uri) {
        mOutputFileUri = uri;
        // 创建thumbnail显示在iv_photo上。并将tip的图标改成删除图标
        HPLog.i("time", "before loadBitmap:" + SystemClock.elapsedRealtime() + uri);
        loadImage(mIvPhoto,uri);
        mIvPhoto.setVisibility(View.VISIBLE);
        mIvTip.setVisibility(View.VISIBLE);
        mIvTip.setImageResource(R.drawable.icon_qualification_delete);
        iv_camare.setVisibility(View.VISIBLE);
        mStatu = Statu.Upload;
    }

    public String getPicUri() {
        return mOutputFileUri;
    }

    public interface UploadClickListener {
        void onUploadClickListener(QualificationPhotoView view);
        void checkBigPitcure(String uri);
    }

}
